﻿using Biotech.Utils;
using project.B12.IService;
using project.B12.Model.BaseModel;
using project.B12.Model.Common;
using project.B12.Model.Enums;
using project.B12.Model.Models;
using project.B12.Model.Res;
using software.Common.MVVM;
using software.dll.user;
using System.Collections.ObjectModel;
using System.Diagnostics.Eventing.Reader;
using System.Windows;
using System.Xml.Linq;

namespace project.B12.Service.UserService
{
    public partial class UserService : ObservableObject, IUserService
    {
        #region Model
        private Dictionary<string, RoleEnum> roleLists = new Dictionary<string, RoleEnum>();
        public Dictionary<string, RoleEnum> RoleLists
        {
            get { return roleLists; }
            set { roleLists = value; this.RaisePropertyChanged(); }
        }
        #endregion
        #region Method
        /// <summary>
        /// 修改角色权限
        /// </summary>
        /// <param name="role"></param>
        /// <param name="rightList"></param>
        /// <returns></returns>
        public MessageModel UpdateRoleRight(RoleEnum role, List<RoleRightModel> rightList)
        {
            try
            {
                if (!IsAdmin())
                {
                    return MessageModel.ReturnFail("Insufficient permissions.");
                }
                var result = new List<string>();
                foreach (var item in rightList)
                {
                    if (item.Flag)
                        result.Add(item.Right.Name);
                }
                var flag = _userManager.UpdateRoleRight(role.ToString(), result);
                if (flag)
                {
                    return MessageModel.ReturnSuccess();//失败
                }
                else
                {
                    return MessageModel.ReturnFail("Permission update failed");//失败
                }
            }
            catch (Exception ex)
            {
                return MessageModel.ReturnFail(ex.ToString());//失败
            }
        }

        /// <summary>
        /// 获取角色权限列表
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public List<RoleRightModel> GetRoleRights(RoleEnum role)
        {
            try
            {
                var rights = _userManager.GetRoleRight(role.ToString());
                var result = _userManager.ShowAllRights()
                    .Select(item => rights.Any(x => x.Rowid == item.Rowid)
                        ? new RoleRightModel(true, item)
                        : new RoleRightModel(false, item))
                    .ToList(); ;
                foreach (var item in result)
                {
                    item.Right.Description = string.Empty;
                    foreach (var item1 in Enum.GetValues(typeof(RightEnum)))
                    {
                        if (item.Right.Name == item1.ToString())
                        {
                            item.Right.Description= EnumUtil.GetEnumDisplay(item1);
                        }
                    }
                }
                return result;
            }
            catch (Exception)
            {
                return new List<RoleRightModel>();//失败
            }
        }

        /// <summary>
        /// 获取角色清单
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, RoleEnum> GetRoleList()
        {
            //通过枚举获取排序规则
            var RoleEnumLis = EnumUtil.GetEnumDisplay<RoleEnum>(typeof(RoleEnum));
            uint Level = 0;//获取当前登录用户的角色等级
            foreach (var item in RoleEnumLis)
            {
                if (item.Value.ToString() == CurrentUser.RoleName)
                {
                    Level = (uint)item.Value;
                }
            }
            // 使用 LINQ 筛选字典项
            return RoleLists = RoleEnumLis
                .Where(pair => (uint)(pair.Value) > Level)
                .ToDictionary(pair => pair.Key, pair => pair.Value);
        }
        #endregion
    }
}
